---
title: "Conjoint_clean_main"
author: "[redacted]"
date: "2025-04-07"
output: html_document
---

```{r}
```

#####LOAD PACKAGES AND DATA

```{r}

#Install packages
#install.packages("tidyverse")
#install.packages("cjoint")
#install.packages("cregg")

#Load packages
library(tidyverse)
library(cjoint)
library(cregg)

#Load data
conjoint_main <- read.qualtrics(
  "data_clean.csv",
  respondentID = "ResponseId",
  letter = "F",
  new.format = TRUE,
  responses = paste0("conjoint_", c("intro", 1:8))
)

#Remove NAs
conjoint_main <- conjoint_main %>% drop_na(selected)

names(conjoint_main)

```

#RENAME VARIABLES

```{r}

# Renaming attributes and levels in English
conjoint_main <-  conjoint_main %>% dplyr::rename(Topic=Onderwerp, 
                                            Relation.to.you=Relatie.tot.u,
                                            Apparent.knowledge.of.the.person=Schijnbare.kennis.van.de.persoon,
                                            Apparent.concern.of.the.person=Schijnbare.bezorgdheid.van.de.persoon,
                                            Apparent.motivation.of.the.person=Schijnbare.motivatie.van.de.persoon,
                                            Closeness.with.you=Band.met.u,
                                            Gender.of.the.person=Geslacht.van.de.persoon
                                            ) %>%
                                     mutate(
                                            Topic = recode_factor(Topic,
                                                  "overstromingen" = "floods", 
                                                  "besmettelijke ziekten" = "infectious diseases",
                                                  "pesticiden" = "pesticides",
                                                  "levensstijlziekten" = "lifestyle diseases",),
                                            Gender.of.the.person = recode_factor(Gender.of.the.person,
                                                  "vrouw" = "female", 
                                                  "man" = "male"),
                                            Relation.to.you = recode_factor(Relation.to.you,
                                                  "vriend/vriendin (platonisch)" = "friend",
                                                  "collega/studiegenoot" = "colleague/fellow student",
                                                  "familielid" = "relative"),
                                            Closeness.with.you = recode_factor(Closeness.with.you,
                                                  "zeer hecht" = "very close", 
                                                  "redelijk hecht" = "fairly close",
                                                  "enigszins hecht" = "somewhat close",
                                                  "niet hecht" = "not close"),
                                            Apparent.knowledge.of.the.person = recode_factor(Apparent.knowledge.of.the.person,
                                                  "zeer goed geinformeerd" = "very well-informed", 
                                                  "redelijk goed geinformeerd" = "fairly well-informed",
                                                  "enigszins geinformeerd" = "somewhat well-informed",
                                                  "niet geinformeerd" = "not well-informed"),
                                            Apparent.concern.of.the.person = recode_factor(Apparent.concern.of.the.person,
                                                  "zeer bezorgd" = "very concerned", 
                                                  "redelijk bezorgd" = "fairly concerned",
                                                  "enigszins bezorgd" = "somewhat concerned",
                                                  "niet bezorgd" = "not concerned"),
                                            Apparent.motivation.of.the.person = recode_factor(Apparent.motivation.of.the.person,
                                                  "u te overtuigen om meer maatregelen te nemen" = "to convince you to take more precautions", 
                                                  "informatie over het risico met u te willen uitwisselen" = "to exchange information with you",
                                                  "zich minder ongerust voelen" = "to feel less anxious",
                                                  "u te overtuigen om minder maatregelen te nemen" = "to convince you to take fewer precautions"))



#Check renamings
table(conjoint_main$Topic)
table(conjoint_main$Gender.of.the.person)
table(conjoint_main$Relation.to.you)
table(conjoint_main$Closeness.with.you)
table(conjoint_main$Apparent.knowledge.of.the.person)
table(conjoint_main$Apparent.concern.of.the.person)
table(conjoint_main$Apparent.motivation.of.the.person)

```

#CHANGE VARIABLE ORDER AND SET REFERENCE CATEGORIES

```{r}

#REORDER LEVELS
conjoint_main$Topic <- factor(conjoint_main$Topic,
                                           levels = c("floods", "pesticides", "infectious diseases", "lifestyle diseases"))
conjoint_main$Gender.of.the.person <- factor(conjoint_main$Gender.of.the.person,
                                           levels = c(  "male", "female"))
conjoint_main$Relation.to.you <- factor(conjoint_main$Relation.to.you,
                                           levels = c("colleague/fellow student", "friend", "relative"))
conjoint_main$Closeness.with.you <- factor(conjoint_main$Closeness.with.you,
                                           levels = c("not close", "somewhat close", "fairly close", "very close"))
conjoint_main$Apparent.knowledge.of.the.person <- factor(conjoint_main$Apparent.knowledge.of.the.person,
                                           levels = c("not well-informed", "somewhat well-informed", "fairly well-informed", "very well-informed"))
conjoint_main$Apparent.concern.of.the.person <- factor(conjoint_main$Apparent.concern.of.the.person,
                                           levels = c("not concerned", "somewhat concerned", "fairly concerned", "very concerned"))
conjoint_main$Apparent.motivation.of.the.person <- factor(conjoint_main$Apparent.motivation.of.the.person,
                                           levels = c("to convince you to take more precautions", "to convince you to take fewer precautions", "to feel less anxious", "to exchange information with you"))

```

#RUN AMCE

```{r}

#### AMCE
amce <- cj(conjoint_main, selected ~ 
             Topic +
             Gender.of.the.person +
             Apparent.motivation.of.the.person +
             Apparent.concern.of.the.person +
             Relation.to.you +
             Closeness.with.you + 
             Apparent.knowledge.of.the.person,
         id = ~Response.ID)

amce_main <- cj(conjoint_main, selected ~ 
             Topic +
             Gender.of.the.person +
             Apparent.motivation.of.the.person +
             Apparent.concern.of.the.person +
             Relation.to.you +
             Closeness.with.you + 
             Apparent.knowledge.of.the.person,
                  id = ~ Response.ID,
                  estimate = "amce")

print(amce)

# Modify the plot syntax to include manual colors for each attribute (feature)
amce_main_plot <- ggplot(amce_main, aes(x = estimate, y = level, color = feature, shape = feature)) +
  geom_point(size = 3) +  # Points with variable shape and size adjusted for clarity
  geom_errorbarh(aes(xmin = lower, xmax = upper), height = 0.5) +  # Error bars colored by feature
  geom_vline(xintercept = 0, linetype = "dashed", color = "black") +  # Vertical line stays black
  theme_minimal() +  # Apply a minimal theme
  labs(x = "AMCE", y = "All attributes", color = "Attribute", shape = "Attribute") +  # Customize labels and legend title
  theme(
    panel.background = element_rect(fill = "white", color = NA),  # Set the background of the plot panel to white
    plot.background = element_rect(fill = "white", color = NA),   # Set the overall background of the plot to white
    legend.position = "right"  # Place the legend on the right
  ) +
  scale_color_manual(values = c(
    "Topic" = "#1b9e77",  # Custom color for Topic
    "Gender.of.the.person" = "darkblue",  # Custom color for Gender
    "Apparent.motivation.of.the.person" = "darkmagenta",   # Custom color for Motivation
    "Apparent.concern.of.the.person" = "deeppink3",  # Custom color for Concern
    "Relation.to.you" = "darkred",  # Custom color for Relationship
    "Closeness.with.you" = "darkorange2",  # Custom color for Closeness
    "Apparent.knowledge.of.the.person" = "goldenrod2"  # Custom color for Knowledgeability
  )) +
    scale_shape_manual(values = c(
    "Topic" = 15, 
    "Gender.of.the.person" = 0, 
    "Relation.to.you" =19, 
    "Closeness.with.you" = 21,  
    "Apparent.knowledge.of.the.person" = 17,
    "Apparent.concern.of.the.person" = 2,  
    "Apparent.motivation.of.the.person" = 18  
  ))+
 guides(color = guide_legend(reverse = TRUE),  # Reverse the order of the color legend
 shape = guide_legend(reverse = TRUE)) +  # Reverse the order of the shape legend
  xlim(-0.1, 0.3)  # Set the x-axis limits (replace with your desired values)

# Save the plot
ggsave("figures/amce_main.png", plot = amce_main_plot, width = 8, height = 6, dpi = 300)

```

#DIAGNOSTICS

```{r}

#DISPLAY FREQUENCIES
# plotting of display frequencies
mainfreqplot <- plot(cj_freqs(conjoint_main, selected ~ 
             Topic +
             Gender.of.the.person +
             Apparent.motivation.of.the.person +
             Apparent.concern.of.the.person +
             Relation.to.you +
             Closeness.with.you + 
             Apparent.knowledge.of.the.person,
             id = ~Response.ID))

mainfreqplot <- mainfreqplot +
   ggtitle("Display Frequencies") +  # Add the title here
   labs(fill = "Attributes") +  # Change 'feature' to a new title
  theme(
    plot.margin = margin(t = 0.5, r = 0.5, b = 1.5, l = 0.5, unit = "cm"),
    text = element_text(size = 16),  # Base text size
    axis.text.y = element_text(size = rel(1)), 
    axis.text.x = element_text(size = rel(1)),
    legend.text = element_text(size = rel(0.85)),
    legend.title = element_text(size = rel(0.95)),
    plot.title = element_text(size = rel(1.05), face = "bold"),  # 5% larger than base
    axis.title = element_text(size = rel(0.95))
  ) +
  guides(fill = guide_legend(nrow = 4, byrow = TRUE)) +
  coord_flip()

# Save the plot with extended height and balanced text
ggsave("diagnostics/freqprop.png", plot = mainfreqplot, width = 11, height = 11, dpi = 300)  # Extended height with balanced text sizes


#LEFT RIGHT DIAGNOSTICS

# Factor the profile variable
conjoint_main$profile_fac <- factor(conjoint_main$profile)

# Calculate marginal means with explicit reference to the outcome (selected)
mainLRplot <- plot(cj(conjoint_main,              
                      selected ~ Topic + 
                                Gender.of.the.person +
                                Apparent.motivation.of.the.person +
                                Apparent.concern.of.the.person +
                                Relation.to.you +
                                Closeness.with.you + 
                                Apparent.knowledge.of.the.person,
                      id = ~Response.ID, 
                      by = ~profile_fac, 
                      estimate = "mm"), 
                   group = "profile_fac", 
                   vline = 0.5)

# Adjust the plot: larger text
mainLRplot_adj <- mainLRplot +
  geom_point(size = 3, position = position_dodge(width = 0.5)) +
    ggtitle("Left/Right Diagnostics") +  # Add the title here
  theme(
    text = element_text(size = 18),            # Increase base text size
    axis.title = element_text(size = 20),      # Increase axis title size
    axis.text = element_text(size = 16),       # Increase axis tick labels size
    legend.title = element_text(size = 20),    # Increase legend title size
    legend.text = element_text(size = 16),     # Increase legend text size
    plot.title = element_text(size = 22, face = "bold"),  # Increase plot title size and style
    strip.text = element_text(size = 18),      # Increase facet labels text size
    plot.margin = margin(15, 15, 15, 15)
  )

# Save the plot with extended height and balanced text
ggsave("diagnostics/LRmain.png", plot = mainLRplot_adj, width = 15, height = 11, dpi = 300)  # Extended height with balanced text sizes

cj_anova(conjoint_main,              
                      selected ~ Topic + 
                                Gender.of.the.person +
                                Apparent.motivation.of.the.person +
                                Apparent.concern.of.the.person +
                                Relation.to.you +
                                Closeness.with.you + 
                                Apparent.knowledge.of.the.person,
                      by = ~profile_fac)

#CARRYOVER EFFECTS

conjoint_main$task_fac <- factor(conjoint_main$task)

# Calculate marginal means with explicit reference to the outcome (selected)
maincarryplot <- plot(cj(conjoint_main,              
                      selected ~ Topic + 
                                Gender.of.the.person +
                                Apparent.motivation.of.the.person +
                                Apparent.concern.of.the.person +
                                Relation.to.you +
                                Closeness.with.you + 
                                Apparent.knowledge.of.the.person,
                      id = ~Response.ID, 
                      by = ~task_fac, 
                      estimate = "mm"), 
                   group = "task_fac", 
                   vline = 0.5)

# Adjust the plot: larger text
maincarryplot_adj <- maincarryplot +
  geom_point(size = 3, position = position_dodge(width = 0.5)) +
    ggtitle("Left/Right Diagnostics") +  # Add the title here
  theme(
    text = element_text(size = 18),            # Increase base text size
    axis.title = element_text(size = 20),      # Increase axis title size
    axis.text = element_text(size = 16),       # Increase axis tick labels size
    legend.title = element_text(size = 20),    # Increase legend title size
    legend.text = element_text(size = 16),     # Increase legend text size
    plot.title = element_text(size = 22, face = "bold"),  # Increase plot title size and style
    strip.text = element_text(size = 18),      # Increase facet labels text size
    plot.margin = margin(15, 15, 15, 15)
  )

# Save the plot with extended height and balanced text
ggsave("diagnostics/carrymain.png", plot = maincarryplot_adj, width = 15, height = 20, dpi = 300)  # Extended height with balanced text sizes

cj_anova(conjoint_main,              
                      selected ~ Topic + 
                                Gender.of.the.person +
                                Apparent.motivation.of.the.person +
                                Apparent.concern.of.the.person +
                                Relation.to.you +
                                Closeness.with.you + 
                                Apparent.knowledge.of.the.person,
                      by = ~task_fac)

```

